package com.spring.format.code.generation.util.file;

import java.util.Date;

import com.spring.format.code.generation.constants.Constants;
import com.spring.format.code.generation.constants.FileNameEnumType;
import com.spring.format.code.generation.mybatis.po.TablePO;
import com.spring.format.code.generation.mybatis.vo.CreateFileVO;
import com.spring.format.code.generation.util.FieldUtil;
import com.spring.format.code.generation.util.NameUtil;
import com.spring.format.code.generation.util.PathUtil;

/**
 * 文件生成帮助类
 * 
 * @author lenovo
 *
 */
public class CommonActionFileUtil {
	private CommonActionFileUtil() {
		
	}
	public static String content(CreateFileVO fileVO) {

		TablePO tablePO = fileVO.getTablePO();
		// vo的类名
		String voName = NameUtil.createFileName(tablePO.getTable_name(), FileNameEnumType.VO_NAME);
		// dao api名称
		String webServiceApiBame = NameUtil.createFileName(tablePO.getTable_name(),
				FileNameEnumType.WEB_SERVICE_API_NAME);
		// dao impl 名称
		String webServiceImplName = NameUtil.createPrivateName(tablePO.getTable_name(),
				FileNameEnumType.WEB_SERVICE_NAME);

		StringBuilder methodStr = new StringBuilder();
		methodStr.append(head(webServiceApiBame, webServiceImplName, tablePO));
		// 数据新增
		methodStr.append(add(voName, webServiceImplName));
		// 数据編輯
		methodStr.append(update(voName, webServiceImplName));
		// 根据id删除数据
		methodStr.append(delete(voName, webServiceImplName));
		// 翻页信息获取
		methodStr.append(page(voName, webServiceImplName));
		// 启用
		methodStr.append(enable(voName, webServiceImplName));
		// 禁用
		methodStr.append(disable(voName, webServiceImplName));
		// 下拉框选择数据
		methodStr.append(ajaxList(voName, webServiceImplName));

		methodStr.append("} //end class");

		return methodStr.toString();

	}

	// 头部代码
	public static String head(String webServiceApiBame, String webServiceImplName, TablePO tablePO) {
		StringBuilder str = new StringBuilder();
		str.append("package ").append(PathUtil.createPackagePath(tablePO.getTable_name(), FileNameEnumType.ACTION_NAME))
				.append("; \n").append("import java.util.List; \n").append("import java.util.Map; \n")
				.append("import org.slf4j.Logger; \n ").append("import org.slf4j.LoggerFactory; \n").append("import ")
				.append(PathUtil.createImportClassPath(tablePO.getTable_name(), FileNameEnumType.WEB_SERVICE_API_NAME))
				.append(";\n")

				.append("/**\n").append("  *").append(tablePO.getTable_comment()).append("\n").append("  *@author ")
				.append(System.getProperties().getProperty("user.name")).append("\n").append("  *@version 1.0.0 ")
				.append(DateUtil.dateFormat(new Date(), DateUtil.DATE_FORMAT)).append(" \n").append("  */\n")
				.append("@RestController \n ").append("@RequestMapping(\"\") \n")

				.append("@Api(value = \"").append(tablePO.getTable_comment()).append("\", tags = { \"")
				.append(tablePO.getTable_comment()).append("\" }) \n").append("public class ")
				.append(NameUtil.createFileName(tablePO.getTable_name(), FileNameEnumType.ACTION_NAME))
				.append("  extends BaseAction{ \n").append("		/*** 日志*/ \n")
				.append("		private Logger log = LoggerFactory.getLogger(this.getClass());\n \n")
				.append("		@Resource\n").append("		private ").append(webServiceApiBame).append(" ")
				.append(webServiceImplName).append(";\n\n").append("		public ").append(webServiceApiBame)
				.append(" ").append(FieldUtil.getMethod(webServiceImplName)).append("(){ \n")
				.append("			return this.").append(webServiceImplName).append(";\n").append("		}\n\n")
				.append("		public void ").append(FieldUtil.setMethod(webServiceImplName)).append("(")
				.append(webServiceApiBame).append(" ").append(webServiceImplName).append("){ \n")
				.append("			 this.").append(webServiceImplName).append(" = ").append(webServiceImplName)
				.append(";\n").append("		}\n \n \n");
		return str.toString();
	}

	// 数据新增
	public static String add(String voName, String webServiceImplName) {
		StringBuilder str = new StringBuilder();
		str.append("	/**数据新增*/ \n").append(" 	@RequestMapping(value=\"save\", method = RequestMethod.POST) \n")
				.append("@ApiOperation(\"\") \n").append(" 	public  ").append(Constants.RESINFO_VO).append("<")
				.append(voName).append(">").append(" add(@RequestBody ").append(voName).append(" vo){ \n")
				.append("      log.debug(\"新增方法调用vo={}\",vo);  \n").append("  		return ")
				.append(webServiceImplName).append(".").append(Constants.SERVICE_METHOD_ADD)
				.append("(vo, loginConsumer()); \n").append(" 	}\n\n\n");
		return str.toString();
	}

	// 数据编辑
	public static String update(String voName, String webServiceImplName) {
		StringBuilder str = new StringBuilder();
		str.append("	/**数据編輯*/ \n").append(" 	@RequestMapping(value=\"update\", method = RequestMethod.POST) \n")
				.append("@ApiOperation(\"\") \n").append(" 	public  ").append(Constants.RESINFO_VO).append("<?>")
				.append(" update(@RequestBody ").append(voName).append(" vo ){ \n")
				.append("      log.debug(\"編輯方法调用vo={}\",vo); \n").append("  		return ")
				.append(webServiceImplName).append(".").append(Constants.SERVICE_METHOD_UPDATE)
				.append("(vo, loginConsumer());\n").append(" 	}\n\n\n");
		return str.toString();
	}

	// 删除
	public static String delete(String voName, String webServiceImplName) {
		StringBuilder str = new StringBuilder();
		str.append(" 	/**根据id删除数据*/\n").append(" 	@RequestMapping(value=\"delete\", method = RequestMethod.POST)\n")
				.append("@ApiOperation(\"\") \n").append(" 	public ").append(Constants.RESINFO_VO)
				.append("<?> delete(@RequestBody ").append(voName).append(" vo){\n ")
				.append("      log.debug(\"删除方法调用vo={}\",vo);  \n").append(" 		return ")
				.append(webServiceImplName).append(".").append(Constants.SERVICE_METHOD_DELETE_BY_ID)
				.append("(vo.getId(),this.loginConsumer());\n").append(" 	}\n\n\n");
		return str.toString();
	}

	// 翻页
	public static String page(String voName, String webServiceImplName) {
		StringBuilder str = new StringBuilder();
		str.append(" 	/**翻页信息获取*/\n").append(" 	@RequestMapping(value=\"page\", method = RequestMethod.POST)\n")
				.append("@ApiOperation(\"\") \n").append(" 	public ").append(Constants.RESINFO_VO).append("<List<")
				.append(voName).append(">> page(@RequestBody ").append(voName).append(" vo){\n")
				.append("      log.debug(\"翻页方法调用vo={}\",vo);  \n").append(" 		return ")
				.append(webServiceImplName).append(".").append(Constants.SERVICE_METHOD_QUERY_PAGE).append("(vo);\n")
				.append(" 	}\n\n\n");
		return str.toString();
	}

	// 启用
	public static String enable(String voName, String webServiceImplName) {
		StringBuilder str = new StringBuilder();
		str.append(" 	/****启用**/ \n").append(" 	@RequestMapping(value=\"enable\" , method = RequestMethod.POST) \n")
				.append("@ApiOperation(\"\") \n").append(" 	public ").append(Constants.RESINFO_VO)
				.append("<?>  enable(@RequestBody ").append(voName).append(" vo){\n ")
				.append("      log.debug(\"启用方法调用vo={}\",vo);  \n").append(" 		  return ")
				.append(webServiceImplName).append(".enable").append("(vo.getId(),loginConsumer()); \n")
				.append(" 	} \n\n\n");
		return str.toString();
	}

	// 禁用
	public static String disable(String voName, String webServiceImplName) {
		StringBuilder str = new StringBuilder();
		str.append(" 	/**** 禁用  **/\n").append(" 	@RequestMapping(value=\"disable\", method = RequestMethod.POST)\n")
				.append("@ApiOperation(\"\") \n").append(" 	public ").append(Constants.RESINFO_VO)
				.append("<?> disable(@RequestBody ").append(voName).append(" vo){\n ")
				.append("      log.debug(\"禁用方法调用vo={}\",vo);  \n").append(" 		   return ")
				.append(webServiceImplName).append(".disable").append("(vo.getId(),loginConsumer());\n")
				.append(" 	} \n \n\n");
		return str.toString();
	}

	// 下拉框选择数据
	public static String ajaxList(String voName, String webServiceImplName) {
		StringBuilder str = new StringBuilder();
		str.append("	/**数据下拉列表**/\n").append("	@RequestMapping(value=\"list\" , method = RequestMethod.POST) \n")
				.append("@ApiOperation(\"数据新增\") \n").append("	public ").append(Constants.RESINFO_VO).append("<List<")
				.append(voName).append(">> list(@RequestBody ").append(voName).append(" vo){\n")
				.append("      log.debug(\"数据下拉列表方法调用vo={}\",vo);  \n").append("			return ")
				.append(webServiceImplName).append(".").append(Constants.SERVICE_METHOD_QUERY_LIST)
				.append("( vo);\n").append("	}\n");
		return str.toString();
	}
}
